//
//  ZSNAudioEffectViewController.m
//  AgoraZAudioBeautyEffectOCZsn
//
//  Created by zhaosuning on 2022/5/29.
//

#import "ZSNAudioEffectViewController.h"
#import <AgoraRtcKit/AgoraRtcEngineKit.h>

#define appid @"6cdc3d89980442faba23c9b3d0a95df0"

#define channelname @"zsncna"

#define token @"0066cdc3d89980442faba23c9b3d0a95df0IADCBZqyrL57iwic+R7o+puhW3vM74zlRFEIUND+a3MDP7X3qvUAAAAAIgDBxArjP3mYYgQAAQA/eZhiAgA/eZhiAwA/eZhiBAA/eZhi"

@interface ZSNAudioEffectViewController () <AgoraRtcEngineDelegate>

@property (strong, nonatomic) AgoraRtcEngineKit *agoraKit;

// 定义 localView 变量
@property (nonatomic, strong) UIView *localView;
// 定义 remoteView 变量
@property (nonatomic, strong) UIView *remoteView;


@end

@implementation ZSNAudioEffectViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"直播页";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIScrollView * bgScrollView = [[UIScrollView alloc] init];
    bgScrollView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
    bgScrollView.showsVerticalScrollIndicator = NO;
    //bgScrollView.backgroundColor = [UIColor greenColor];
    bgScrollView.backgroundColor = [UIColor whiteColor];
    bgScrollView.clipsToBounds = YES;
    bgScrollView.bounces = YES;
    bgScrollView.alwaysBounceVertical = YES;
    bgScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.view addSubview:bgScrollView];
    
    UIView * dibuView = [[UIView alloc] initWithFrame:CGRectZero];
    dibuView.clipsToBounds = YES;
    //dibuView.backgroundColor = [UIColor redColor];
    dibuView.backgroundColor = [UIColor whiteColor];
    dibuView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
    [bgScrollView addSubview:dibuView];
    
    UIButton *btnJoinChannel = [[UIButton alloc] init];
    btnJoinChannel.frame = CGRectMake(245, 185, 100, 40);
    [btnJoinChannel setTitle:@"进入频道" forState:UIControlStateNormal];
    btnJoinChannel.backgroundColor = [UIColor redColor];
    //[btnJoinChannel setTitleColor:[UIColor colorWithRed:108/255.0 green:210/255.0 blue:180/255.0 alpha:1.0] forState:UIControlStateNormal];
    
    [btnJoinChannel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    btnJoinChannel.titleLabel.font = [UIFont systemFontOfSize:20.0];
    
    [btnJoinChannel addTarget:self action:@selector(btnJoinChannelClick:) forControlEvents:UIControlEventTouchUpInside];
    [dibuView addSubview:btnJoinChannel];
    
    
    UIButton *btnLeaveChannel = [[UIButton alloc] init];
    btnLeaveChannel.frame = CGRectMake(125, 185, 100, 40);
    [btnLeaveChannel setTitle:@"离开频道" forState:UIControlStateNormal];
    btnLeaveChannel.backgroundColor = [UIColor redColor];
    [btnLeaveChannel setTitleColor:[UIColor colorWithRed:108/255.0 green:210/255.0 blue:180/255.0 alpha:1.0] forState:UIControlStateNormal];
    btnLeaveChannel.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [btnLeaveChannel addTarget:self action:@selector(btnLeaveChannelClick:) forControlEvents:UIControlEventTouchUpInside];
    [dibuView addSubview:btnLeaveChannel];
    
    
    UIButton *btnDestroy = [[UIButton alloc] init];
    btnDestroy.frame = CGRectMake(5, 185, 100, 40);
    [btnDestroy setTitle:@"销毁" forState:UIControlStateNormal];
    btnDestroy.backgroundColor = [UIColor redColor];
    [btnDestroy setTitleColor:[UIColor colorWithRed:108/255.0 green:210/255.0 blue:180/255.0 alpha:1.0] forState:UIControlStateNormal];
    btnDestroy.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [btnDestroy addTarget:self action:@selector(btnDestroyClick:) forControlEvents:UIControlEventTouchUpInside];
    [dibuView addSubview:btnDestroy];
    
    self.localView = [[UIView alloc] init];
    self.localView.backgroundColor = [UIColor yellowColor];
    self.localView.frame = CGRectMake(5, 20, 150, 150);
    [dibuView addSubview:self.localView];
    
    self.remoteView = [[UIView alloc] init];
    self.remoteView.frame = CGRectMake(175, 20, 150, 150);
    self.remoteView.backgroundColor = [UIColor blueColor];
    [dibuView addSubview:self.remoteView];
    
    UILabel *lblVoiceBeauty = [[UILabel alloc] init];
    lblVoiceBeauty.text = @"美声";
    lblVoiceBeauty.font = [UIFont boldSystemFontOfSize:17];
    //lblVoiceBeauty.backgroundColor = [UIColor yellowColor];
    lblVoiceBeauty.frame = CGRectMake(20, 230, 100, 40);
    [dibuView addSubview:lblVoiceBeauty];
    
    UIButton *btnCloseAudioBeauty = [[UIButton alloc] init];
    btnCloseAudioBeauty.frame = CGRectMake(140, 230, 100, 40);
    [btnCloseAudioBeauty setTitle:@"关闭美声" forState:UIControlStateNormal];
    btnCloseAudioBeauty.backgroundColor = [UIColor redColor];
    [btnCloseAudioBeauty setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    btnCloseAudioBeauty.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [btnCloseAudioBeauty addTarget:self action:@selector(btnCloseAudioBeautyClick:) forControlEvents:UIControlEventTouchUpInside];
    [dibuView addSubview:btnCloseAudioBeauty];
    
    UIButton *btnAudioBeautyXiangQin = [[UIButton alloc] init];
    btnAudioBeautyXiangQin.frame = CGRectMake(5, 280, 100, 40);
    [btnAudioBeautyXiangQin setTitle:@"语音相亲" forState:UIControlStateNormal];
    btnAudioBeautyXiangQin.backgroundColor = [UIColor redColor];
    [btnAudioBeautyXiangQin setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    btnAudioBeautyXiangQin.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [btnAudioBeautyXiangQin addTarget:self action:@selector(btnAudioBeautyXiangQinClick:) forControlEvents:UIControlEventTouchUpInside];
    [dibuView addSubview:btnAudioBeautyXiangQin];
    
    UIButton *btnAudioSingingBeauty = [[UIButton alloc] init];
    btnAudioSingingBeauty.frame = CGRectMake(125, 280, 100, 40);
    [btnAudioSingingBeauty setTitle:@"歌唱美声" forState:UIControlStateNormal];
    btnAudioSingingBeauty.backgroundColor = [UIColor redColor];
    [btnAudioSingingBeauty setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    btnAudioSingingBeauty.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [btnAudioSingingBeauty addTarget:self action:@selector(btnAudioSingingBeautyClick:) forControlEvents:UIControlEventTouchUpInside];
    [dibuView addSubview:btnAudioSingingBeauty];
    
    UIButton *btnChangeRole = [[UIButton alloc] init];
    btnChangeRole.frame = CGRectMake(125, 340, 100, 40);
    [btnChangeRole setTitle:@"切为观众" forState:UIControlStateNormal];
    btnChangeRole.backgroundColor = [UIColor redColor];
    [btnChangeRole setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    btnChangeRole.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [btnChangeRole addTarget:self action:@selector(btnChangeRoleClick:) forControlEvents:UIControlEventTouchUpInside];
    [dibuView addSubview:btnChangeRole];
    
    UIButton *btnChangeRoleBroadcaster = [[UIButton alloc] init];
    btnChangeRoleBroadcaster.frame = CGRectMake(125, 390, 100, 40);
    [btnChangeRoleBroadcaster setTitle:@"切为主播" forState:UIControlStateNormal];
    btnChangeRoleBroadcaster.backgroundColor = [UIColor redColor];
    [btnChangeRoleBroadcaster setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    btnChangeRoleBroadcaster.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [btnChangeRoleBroadcaster addTarget:self action:@selector(btnChangeRoleBroadcasterClick:) forControlEvents:UIControlEventTouchUpInside];
    [dibuView addSubview:btnChangeRoleBroadcaster];
    
}

//切换角色
-(void) btnChangeRoleClick:(id)sender {
    [self.agoraKit setClientRole:AgoraClientRoleAudience];
}

-(void) btnChangeRoleBroadcasterClick:(id)sender {
    [self.agoraKit setClientRole:AgoraClientRoleBroadcaster];
}


//AgoraSingingBeautifier 歌唱美声
-(void) btnAudioSingingBeautyClick:(id)sender {
    //[_agoraKit setVoiceBeautifierPreset:AgoraSingingBeautifier];
    
    [_agoraKit setVoiceBeautifierParameters:AgoraSingingBeautifier param1:2 param2:3];
    
    // 预设人声效果为电音音效。
//    [self.agoraKit setAudioEffectPreset: AgoraPitchCorrection];
//
//    // 将电音音效设置为主音音高为 D 的自然小调。
//    [self.agoraKit setAudioEffectParameters: AgoraPitchCorrection param1:2 param2:6];
    
    NSLog(@"打印了 歌唱美声AgoraSingingBeautifier");
}

//关闭美声
-(void) btnCloseAudioBeautyClick:(id)sender {
    [_agoraKit setVoiceBeautifierPreset:AgoraVoiceBeautifierOff];
    NSLog(@"打印了 关闭美声");
}


//语聊美声--语音相亲
- (void) btnAudioBeautyXiangQinClick:(id)sender {
    [_agoraKit setVoiceBeautifierPreset:AgoraChatBeautifierMagnetic];
    NSLog(@"打印了 磁性 男 AgoraChatBeautifierMagnetic");
}

//加入频道
- (void)btnJoinChannelClick:(id)sender {
    AgoraRtcEngineConfig * config = [[AgoraRtcEngineConfig alloc] init];
    config.appId = appid;
    config.areaCode = 0xFFFFFFFF;
    
    AgoraLogConfig *logConfig = [[AgoraLogConfig alloc] init];
    logConfig.level = AgoraLogLevelInfo;
    config.logConfig = logConfig;
    
    self.agoraKit = [AgoraRtcEngineKit sharedEngineWithConfig:config delegate:self];
    [self.agoraKit setChannelProfile:AgoraChannelProfileLiveBroadcasting];
    [self.agoraKit setAudioProfile:AgoraAudioProfileMusicHighQuality scenario:AgoraAudioScenarioGameStreaming];
    
    //AgoraClientRoleOptions *clientRoleOption = [[AgoraClientRoleOptions alloc] init];
    //clientRoleOption.audienceLatencyLevel = AgoraAudienceLatencyLevelUltraLowLatency;
    [self.agoraKit setClientRole:AgoraClientRoleBroadcaster];
    //[self.agoraKit setClientRole:AgoraClientRoleBroadcaster options:clientRoleOption];
    
    //[self.agoraKit setClientRole:AgoraClientRoleAudience];
    
    [self.agoraKit enableAudio];
    [self.agoraKit enableVideo];
    
    AgoraRtcChannelMediaOptions *mediaOption = [[AgoraRtcChannelMediaOptions alloc] init];
    NSInteger result = [self.agoraKit joinChannelByToken:token channelId:channelname info:nil uid:0 options:mediaOption];
    if (result != 0) {
        NSLog(@"打印了 进入频道失败 %ld",(long)result);
    }
    else {
        NSLog(@"打印了 进入频道返回0");
    }
}
-(void) setupLocalVideo {
    AgoraRtcVideoCanvas *videoCanvas = [[AgoraRtcVideoCanvas alloc] init];
    videoCanvas.uid = 0;
    videoCanvas.renderMode = AgoraVideoRenderModeHidden;
    videoCanvas.view = self.localView;
    [self.agoraKit setupLocalVideo:videoCanvas];
}

-(void)setupRemoteVideo: (NSInteger) uid {
    AgoraRtcVideoCanvas *videoCanvas = [[AgoraRtcVideoCanvas alloc] init];
    videoCanvas.uid = uid;
    videoCanvas.renderMode = AgoraVideoRenderModeHidden;
    videoCanvas.view = self.remoteView;
    [self.agoraKit setupRemoteVideo:videoCanvas];
}

//离开频道
- (void)btnLeaveChannelClick:(id)sender {
    [self.agoraKit leaveChannel:^(AgoraChannelStats * _Nonnull stat) {
        NSLog(@"打印了 离开频道 %@",stat);
    }];
    
    
}

//销毁
- (void)btnDestroyClick:(id)sender {
    [AgoraRtcEngineKit destroy];
}



- (void)rtcEngine:(AgoraRtcEngineKit *)engine didOccurWarning:(AgoraWarningCode)warningCode {
    
}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine didOccurError:(AgoraErrorCode)errorCode {
    
}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine didJoinChannel:(NSString *)channel withUid:(NSUInteger)uid elapsed:(NSInteger)elapsed {
    [self setupLocalVideo];
}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine didJoinedOfUid:(NSUInteger)uid elapsed:(NSInteger)elapsed {
    [self setupRemoteVideo:uid];
}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine didOfflineOfUid:(NSUInteger)uid reason:(AgoraUserOfflineReason)reason {
    
}


@end
